from google.appengine.ext import db
# Model for Beer
class Beer(db.Model):
	name = db.StringProperty(required=False);
	image = db.StringProperty();
	style = db.StringProperty();
	description = db.StringProperty();
	abv = db.FloatProperty();
	ibu = db.FloatProperty();
	rating = db.FloatProperty();
	brewery_id = db.IntegerProperty(required=False)
	brewery_name = db.StringProperty();


	def to_dict(self):
		SIMPLE_TYPES = (int, long, float, bool, dict, basestring, list)
		output = {}
		output["ID"] = self.key().id()
		for key, prop in self.properties().iteritems():
			value = getattr(self, key)
			if isinstance(value, SIMPLE_TYPES):
				output[key] = value
			elif value is None:
				output[key] = ""
			elif isinstance(value, datetime.date):
				# Convert date/datetime to ms-since-epoch ("new Date()").
				ms = time.mktime(value.utctimetuple())
				ms += getattr(value, 'microseconds', 0) / 1000
				output[key] = int(ms)
			elif isinstance(value, db.Model):
				output[key] = to_dict(value)
			else:
				raise ValueError('cannot encode ' + repr(prop))
		return output
		#return simplejson.dumps(output).replace("\/", "/")

